package com.helloworld.demo.sync;

public class HelloSynchrnzied {

    static int count = 0;

    private static String lock = "";

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5000; i++) {
                    increament();
                }
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5000; i++) {
                    decreament();
                }
            }
        });

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();
        System.out.println(count);

    }

    static void increament(){
        synchronized (lock){
            count++;
        }
    }

    static void decreament(){
        synchronized (lock){
            count--;
        }
    }

}
